﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using m_pricelist.Properties;
using System.Runtime.InteropServices;
using System.Data.OleDb;
using m_pricelist._DAL_;
using m_pricelist._BAL_;


namespace m_pricelist
{
    public partial class FreightConfigurationForm : Form 
    {
        private int m_lFreight1;
        private int m_lFreight2;
        private int m_lFreight3;
        private int m_lFreight4;
        private int m_lFreight5;
        private int m_lFreight6;
        private int m_lFreight7;
        private int m_lFreight8;
        private int m_lFreight9;
        private int m_lMarelIns;
        private int m_lAEWDFreight;
        private int m_lAEWDins;
        private int m_lCTfreight;
        private int m_lCTins;
        private int m_lFTfreight;
        private int m_lFTins;
        private string m_sDeliveryTerms;
        private string m_sShippingRef;
        private string m_sAEWDShippingRef;
        private string m_sDTShippingRef;
        private string m_sCTShippingRef;
        private string m_sPriceListCurrency;
        private Type_ShippingParameter m_tShippingParam = new Type_ShippingParameter();

        private Type_Installation ts;

        public FreightConfigurationForm()
        {
            InitializeComponent();
            UserForm_Initialize_Freight();
        }

        private void FreightConfigurationForm_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.FromArgb(0, 44, 83);
        }

        //Upphafsstillir formið
        public void UserForm_Initialize_Freight()
        {
            int lRow = 0;
            int lR = 0;
            double dForwardCost = 0;
            double dAEWDForwardCost = 0;
            double dDTForwardCost = 0;
            string sPriceListCurrency = null;
            string Prufa = null;
            int fjoldiRada = 0;

            //-----Þetta eru rammarnir í forminu. Þetta er upphafsstilling þeirra---------//
            frmDeliveryTerms2.Enabled = false;
            frmFirstPort.Enabled = false;
            frmFinalDest.Enabled = false;
            //-------------//

            Excel.Application xlApp = null;
            Excel.Workbook xlWorkBook = null;
            Excel.Worksheet xlSheet = null;
            xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
            xlWorkBook = Globals.ThisAddIn.Application.ActiveWorkbook;
            xlSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            //hér er verið að kalla í lsbIn sem er Listboxið í þessu formi. List skipunin virkar ekki, hana þarf að skoða betur

            for (lRow = 2; lRow <= 30; lRow++)
            {
                if (xlWorkBook.Sheets["SolutionC"].Cells(lRow, 10).Value > null) //Excel
                {
                    if (xlWorkBook.Sheets["SolutionC"].Cells(lRow, 12).Value > null) //Excel
                    {
                        lsbIn.Items.Add("");
                        lsbIn = xlWorkBook.Sheets["SolutionC"].Cells(lRow, 1).Value;
                        //Todo, fá þetta til að virka almennilega
                        //Fær gildi sellnanna úr Excel í listboxið
                        //lsbIn.List(lsbIn.ListCount - 1, 0) = xlWorkBook.Sheets["SolutionC"].Cells(lRow, 1).Value;
                        //lsbIn.Items.Count - 1
                        //Veit ekki alveg hvort þetta er rétt
                        lsbIn.Items.Insert(0, Convert.ToString(xlWorkBook.Sheets["SolutionC"].Cells(lRow, 1).Value));
                        lsbIn.Items.Insert(1, Convert.ToString(xlWorkBook.Sheets["SolutionC"].Cells(lRow, 11).Value));
                        lsbIn.Items.Insert(2, Convert.ToString(xlWorkBook.Sheets["SolutionC"].Cells(lRow, 12).Value));
                    }
                    else
                    {
                        lsbOut.Items.Add(xlWorkBook.Sheets["SolutionC"].Cells(lRow, 1).Value);
                    }
                }
            }

            //Það á eftir að útfæra sprQuote í Quote forminu

            #region sprQuote - Á eftir að útfæra í Quote forminu
            //Prufa
            ProductsData prData = new ProductsData();
            DataSet donni;
            donni = prData.getSprQuoteItems(Prufa);
            Products prodobj = new Products();
            fjoldiRada = prodobj.excelRowCounter(xlWorkBook); 


            //TODO Það á eftir að útfæra sprQuote dataGriddið
            for (lRow = 1; lRow < fjoldiRada; lRow++)//TODO þetta ætti að vera itemnumber  röðin í excelskjalinu en ekki sprq
            {

                //if (prodobj. TextMatrix(lRow, 2) == "800-0000-freight_5")//(frmQuote.sprQuote.TextMatrix(lRow, 2) == "800-0000-freight_5")
                //{
                //    m_lFreight5 = lRow;
                //}
                //else if (frmQuote.sprQuote.TextMatrix(lRow, 2) == "800-0000-freight_4")
                //{
                //    m_lFreight4 = lRow;
                //}
                //else if (frmQuote.sprQuote.TextMatrix(lRow, 2) == "800-0000-freight_3")
                //{
                //    m_lFreight3 = lRow;
                //}
                //else if (frmQuote.sprQuote.TextMatrix(lRow, 2) == "800-0000-freight_2")
                //{
                //    m_lFreight2 = lRow;
                //}
                //else if (frmQuote.sprQuote.TextMatrix(lRow, 2) == "800-0000-freight_1")
                //{
                //    m_lFreight1 = lRow;
                //}
                //else if (frmQuote.sprQuote.TextMatrix(lRow, 2) == "800-0000-freight_6")
                //{
                //    m_lFreight6 = lRow;
                //}
                //else if (frmQuote.sprQuote.TextMatrix(lRow, 2) == "800-0000-freight_7")
                //{
                //    m_lFreight7 = lRow;
                //}
                //else if (frmQuote.sprQuote.TextMatrix(lRow, 2) == "800-0000-freight_8")
                //{
                //    m_lFreight8 = lRow;
                //}
                //else if (frmQuote.sprQuote.TextMatrix(lRow, 2) == "800-0000-freight_9")
                //{
                //    m_lFreight9 = lRow;
                //}
                //else if (frmQuote.sprQuote.TextMatrix(lRow, 2) == "800-0000-ins")
                //{
                //    m_lMarelIns = lRow;
                //}
            }
            #endregion

            m_sPriceListCurrency = Convert.ToString(xlWorkBook.CustomDocumentProperties("Currency").Value);
            m_sDeliveryTerms = Convert.ToString(xlWorkBook.CustomDocumentProperties("DeliveryTerms").Value.Trim(' '));

            lblCurrency1.Name = "Cost in " + m_sPriceListCurrency;
            //lblCurrency2 .Caption = "Insurance in " + m_sPriceListCurrency; // finn ekki lblCurrency2, veit ekki hvar það á að vera.
            lblCurrency3.Name = "Cost in " + m_sPriceListCurrency;
            lblCurrency4.Name = "Insurance in " + m_sPriceListCurrency;
            lblCurrency5.Name = "Total Freight Cost in " + m_sPriceListCurrency;

            if (xlWorkBook.CustomDocumentProperties("ShippingStatus").Value.Trim(' ') == "Calculate")
            {
                chkCalShipping.Checked = true;
            }
            else
            {
                chkCalShipping.Checked = false;
            }

            switch (m_sDeliveryTerms)
            {
                //Fer í föllinn eins og optRegOffer2
                case "FOB":
                    optRegOffer_CheckedChanged(this, EventArgs.Empty);
                    optFOB.Checked = true;
                    break;
                case "CFR":
                    optRegOffer_CheckedChanged(this, EventArgs.Empty);
                    optCFRFreight.Checked = true; //- Veit ekki hvaðan þetta kemur
                    break;
                case "CIF":
                    optRegOffer_CheckedChanged(this, EventArgs.Empty);
                    optCifFreight.Checked = true; //- Veit ekki hvaðan þetta kemur
                    break;
                case "FCA":
                    optRegOffer2_CheckedChanged(this, EventArgs.Empty);
                    optFCA_CheckedChanged(this, EventArgs.Empty);
                    break;
                case "CPT":
                    optRegOffer2_CheckedChanged(this, EventArgs.Empty);
                    optCPT.Checked = true;
                    break;
                case "CIP":
                    optRegOffer2_CheckedChanged(this, EventArgs.Empty);
                    optCIP.Checked = true;
                    break;
                case "DAP":
                    optRegOffer2_CheckedChanged(this, EventArgs.Empty);
                    optDAP.Checked = true;
                    break;
                case "DAT":
                    optRegOffer2_CheckedChanged(this, EventArgs.Empty);
                    optDAT.Checked = true;
                    break;
            }

            //Hér er kallað í eitthvað í Quoteforminu
            m_tShippingParam.lId = 1;
            if (m_lFreight5 > 0) //Kallar í sprQuote sem er ekki tilbúið í Quote forminu
            {
                //m_tShippingParam.d20ContainerPriceEurope = Convert.ToDouble(frmQuote.sprQuote.TextMatrix(m_lFreight5, 4));
                //m_tShippingParam.l20ContainerNumberEurope = Convert.ToInt64(frmQuote.sprQuote.TextMatrix(m_lFreight5, 5));
            }
            else
            {
                m_tShippingParam.d20ContainerPriceEurope = ContainerPrice("800-0000-freight_5"); //Ekki er víst að ContainerPrice fallið virki.
            }
            if (m_lFreight4 > 0)
            {
                //m_tShippingParam.d40ContainerPriceEurope = Convert.ToDouble(frmQuote.sprQuote.TextMatrix(m_lFreight4, 4));
                //m_tShippingParam.l40ContainerNumberEurope = Convert.ToInt64(frmQuote.sprQuote.TextMatrix(m_lFreight4, 5));
            }
            else
            {
                m_tShippingParam.d40ContainerPriceEurope = ContainerPrice("800-0000-freight_4");
            }
            if (m_lFreight3 > 0)
            {
                //m_tShippingParam.d20ContainerPriceUsa = Convert.ToDouble(frmQuote.sprQuote.TextMatrix(m_lFreight3, 4));
                //m_tShippingParam.l20ContainerNumberUsa = Convert.ToInt64(frmQuote.sprQuote.TextMatrix(m_lFreight3, 5));
            }
            else
            {
                m_tShippingParam.d20ContainerPriceUsa = ContainerPrice("800-0000-freight_3");
            }
            if (m_lFreight2 > 0)
            {
                //m_tShippingParam.d40ContainerPriceUsa = Convert.ToDouble(frmQuote.sprQuote.TextMatrix(m_lFreight2, 4));
                //m_tShippingParam.l40ContainerNumberUsa = Convert.ToInt64(frmQuote.sprQuote.TextMatrix(m_lFreight2, 5));
            }
            else
            {
                m_tShippingParam.d40ContainerPriceUsa = ContainerPrice("800-0000-freight_2");
            }

            if (m_lFreight1 > 0)
            {
                //m_tShippingParam.dContainerFinalPrice = Convert.ToDouble(frmQuote.sprQuote.TextMatrix(m_lFreight1, 4));
                //m_tShippingParam.lContainerFinalNumber = Convert.ToInt64(frmQuote.sprQuote.TextMatrix(m_lFreight1, 5));
            }


            if (m_lFreight6 > 0)
            {
                //m_tShippingParam.d20FOB = Convert.ToDouble(frmQuote.sprQuote.TextMatrix(m_lFreight6, 4));
                //m_tShippingParam.l20ContainerNumberEurope = Convert.ToInt64(frmQuote.sprQuote.TextMatrix(m_lFreight6, 5));
            }
            else
            {
                m_tShippingParam.d20FOB = ContainerPrice("800-0000-freight_6");
            }
            if (m_lFreight7 > 0)
            {
                //m_tShippingParam.d40FOB = Convert.ToDouble(frmQuote.sprQuote.TextMatrix(m_lFreight7, 4));
                //m_tShippingParam.l40ContainerNumberEurope = Convert.ToInt64(frmQuote.sprQuote.TextMatrix(m_lFreight7, 5));
            }
            else
            {
                m_tShippingParam.d40FOB = ContainerPrice("800-0000-freight_7");
            }
            if (m_lFreight8 > 0)
            {
                //m_tShippingParam.d20FCA = Convert.ToDouble(frmQuote.sprQuote.TextMatrix(m_lFreight8, 4));
                //m_tShippingParam.l20ContainerNumberEurope = Convert.ToInt64(frmQuote.sprQuote.TextMatrix(m_lFreight8, 5));
            }
            else
            {
                m_tShippingParam.d20FCA = ContainerPrice("800-0000-freight_8");
            }
            if (m_lFreight9 > 0)
            {
                //m_tShippingParam.d40FCA = Convert.ToDouble(frmQuote.sprQuote.TextMatrix(m_lFreight9, 4));
                //m_tShippingParam.l40ContainerNumberEurope = Convert.ToInt64(frmQuote.sprQuote.TextMatrix(m_lFreight9, 5));
            }
            else
            {
                m_tShippingParam.d40FCA = ContainerPrice("800-0000-freight_9");
            }

            double dCFIprocent_2 = 0.006;
            //Þetta þýðist ekki, kemur villa - Unable to find WorksheetFunction
            m_tShippingParam.dCIFprocent = dCFIprocent_2;
            //item
            //m_tShippingParam.dEquipmentCost = xlApp.WorksheetFunction.SumIfs(xlWorkBook.Sheets["Output"].Range("G:G"), xlWorkBook.Sheets["Output"].Range("W:W"), "I");
            ////option 
            //m_tShippingParam.dEquipmentCost = m_tShippingParam.dEquipmentCost + xlApp.WorksheetFunction.SumIfs(xlWorkBook.Sheets["Output"].Range("G:G"), xlWorkBook.Sheets["Output"].Range("W:W"), "0");

            switch (m_sDeliveryTerms)
            {
                case "FCA":
                    m_tShippingParam.lCIFNumber = -1;
                    break;
                case "FOB":
                    m_tShippingParam.lCIFNumber = -1;
                    break;
                case "CIF":
                    m_tShippingParam.lCIFNumber = 1;
                    break;
                case "CIP":
                    m_tShippingParam.lCIFNumber = 1;
                    break;
                default:
                    m_tShippingParam.lCIFNumber = 0;
                    break;
            }

            txt20FeetContainer.Text = Convert.ToString((m_tShippingParam.l20ContainerNumberEurope > m_tShippingParam.l20ContainerNumberUsa) ? m_tShippingParam.l20ContainerNumberEurope : m_tShippingParam.l20ContainerNumberUsa);
            txt40feetContainer.Text = Convert.ToString((m_tShippingParam.l40ContainerNumberEurope > m_tShippingParam.l40ContainerNumberUsa) ? m_tShippingParam.l40ContainerNumberEurope : m_tShippingParam.l40ContainerNumberUsa);
            txtFinalAdress.Text = xlWorkBook.CustomDocumentProperties("FinalDest").Value.Trim(' ');
            cboFirstPort.Text = xlWorkBook.CustomDocumentProperties("FirstPort").Value.Trim(' ');

            if (m_tShippingParam.lCIFNumber != -1)
            {
                optFirstPortEurope.Text = Convert.ToString((m_tShippingParam.l20ContainerNumberEurope > 0) ? true : false);
                if (!optFirstPortEurope.Checked)
                {
                    optFirstPortEurope.Text = Convert.ToString((m_tShippingParam.l40ContainerNumberEurope > 0) ? true : false);
                }

                optFirstPortUS.Text = Convert.ToString((m_tShippingParam.l20ContainerNumberUsa > 0) ? true : false);
                if (!optFirstPortUS.Checked)
                {
                    optFirstPortUS.Text = Convert.ToString((m_tShippingParam.l40ContainerNumberUsa > 0) ? true : false);
                }
            }

            dForwardCost = Convert.ToDouble(m_tShippingParam.lContainerFinalNumber) * m_tShippingParam.dContainerFinalPrice;

            CalculateFirstPortCost();
            CalculateCIFCost();
            CalculateShippingCost();

        }//UserFormInitallize endar

        private void cboFirstPort_SelectedIndexChanged(object sender, EventArgs e) //Þetta fall er tilbúið - 11.10.2012
        {
            try
            {
                m_tShippingParam.sFirstPort = cboFirstPort.Text;

                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        // Færir gögn frá vinstra listboxinu yfir í það hægra meginn.
        private void cmdAdd_Click(object sender, EventArgs e) //05.10.2012 - Ætti að virka
        {
            #region Gamli kóðinn
            //Gamli kóðinn
            //if (lsbOut.Items.Count != -1)
            //{
            //    if (txtFwCost.Text == "")
            //    {
            //        MessageBox.Show("Forward cost is missing");
            //        return;
            //    }
            //    lsbIn.Items.Add("");  //.AddItem "";
            //    lsbIn.List(lsbIn.ListCount - 1, 0) = lsbOut.List(lsbOut.ListIndex, 0);
            //    lsbIn.List(lsbIn.ListCount - 1, 1) = lsbOut.List(lsbOut.ListIndex, 0) + "(Ref." + txtFwRef.Text + ")";
            //    lsbIn.List(lsbIn.ListCount - 1, 2) = txtFwCost.Text;

            //    lsbOut.RemoveItem(lsbOut.ListIndex);
            //    txtFwCost.Text = "";
            //    txtFwRef.Text = "";
            //    CalculateShippingCost();
            //}

            // nýji kóðinn
            #endregion

            try
            {
                if (lsbOut.SelectedIndex > -1)
                {
                    if (txtFwCost.Text == "")
                    {
                        MessageBox.Show("Forward cost is missing");
                        return;
                    }
                    lsbIn.Items.Add("");  //.AddItem "";

                    string _Value = lsbOut.SelectedItem.ToString() + "(Ref." + txtFwRef.Text + ")" + txtFwCost; //Gets the value of  items in list.
                    lsbOut.Items.Add(_Value); //Add the list item to the selected list
                    lsbIn.Items.Remove(_Value); //Remove the details from the list

                    txtFwCost.Text = "";
                    txtFwRef.Text = "";
                    CalculateShippingCost();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }         
        }

        //Remove takkinn, tekur items úr lsbIn og færir yfir í lsbOut
        private void cmdRemove_Click(object sender, EventArgs e)
        {
            try
            {
                if (lsbIn.SelectedIndex > -1)
                {
                    string _Value = lsbOut.SelectedItem.ToString(); //Gets the value of items in list.
                    lsbIn.Items.Add(_Value); //Add the list item to the selected list 
                    lsbOut.Items.Remove(_Value); //Remove the details from the other list  
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
           
        }

        // Clear takkinn í Freight
        private void cmdClearSc_Click(object sender, EventArgs e) //Þetta fall ætti að virka 11.10.2012
        {
            try
            {
                optFirstPortEurope.Checked = false;
                optFirstPortUS.Checked = false; //Veit ekki hvaðan þessi kemur. Er útfærður til bráðabirgða í Freight
                optCFRFreight.Checked = false; //Veit ekki hvaðan þessi kemur. Er útfærður til bráðabirgða í Freight
                optCifFreight.Checked = false; //Veit ekki hvaðan þessi kemur. Er útfærður til bráðabirgða í Freight
                optFOB.Checked = false;

                optFCA.Checked = false;
                optCPT.Checked = false;
                optCIP.Checked = false;
                optDAP.Checked = false;
                optDAT.Checked = false;

                cboFirstPort.Text = "";
                cboFirstPort.Items.Clear();

                txt20FeetContainer.Text = "";
                txt40feetContainer.Text = "";

                txtFinalAdress.Text = "";
                lblCIFFreightCost.Text = "";
                lblFirstPortCost.Text = "";
                lblTotalCost.Text = "";

                m_tShippingParam.sFinalDest = " ";
                m_tShippingParam.sFirstPort = " ";
                m_tShippingParam.dContainerFinalPrice = 0;
                m_tShippingParam.l20ContainerNumberEurope = 0;
                m_tShippingParam.l20ContainerNumberUsa = 0;
                m_tShippingParam.l40ContainerNumberEurope = 0;
                m_tShippingParam.l40ContainerNumberUsa = 0;
                m_tShippingParam.lCIFNumber = -1;
                m_tShippingParam.lContainerFinalNumber = 0;
                m_tShippingParam.dAEWDFinalPrice = 0;
                m_tShippingParam.lAEWDFinalNumber = 0;
                m_tShippingParam.lph1 = 0;
                m_tShippingParam.lph2 = 0;
                m_tShippingParam.lph3 = 0;
                m_tShippingParam.lph4 = 0;
                m_tShippingParam.sShippingFormStatus = "N/A";

                optRegOffer2.Checked = true;
                optFCA.Checked = true;
                optFCA_CheckedChanged(this, EventArgs.Empty);
                chkCalShipping.Checked = false;
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //Þegar smellt er á OK takkann í forminu.
        private void cmdOKsc_Click(object sender, EventArgs e) //Þetta fall er ekki alveg tilbúið. Það er kallað í sprQuote sem þarf að útfæra fyrst. 12.10.2012
        {
            Excel.Application xlApp = null;
            Excel.Workbook xlWorkBook = null;
            Excel.Worksheet xlSheet = null;
            xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
            xlWorkBook = Globals.ThisAddIn.Application.ActiveWorkbook;
            xlSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            try
            {
                int l = 0;
                int lRow = 0;
                Range rFundin = null;
                double dCommission = 0;
                bool bFreightExist = false;
                int lExtraRow = 0;
                int lFreightCount = 0;

                int cItotal = 7; //Global breyta úr VB - Column number

                if ((m_tShippingParam.sDeliveryTerms != "FOB") & (m_tShippingParam.sDeliveryTerms != "FCA"))
                {
                    if ((optRegOffer2.Checked) & (m_tShippingParam.sFinalDest.Trim(' ') == ""))
                    {
                        MessageBox.Show("Final destination is missing!");
                        return;
                    }
                    else if ((m_tShippingParam.sFinalDest.Trim(' ') == "") & (m_tShippingParam.sFirstPort.Trim(' ') == ""))
                    {
                        MessageBox.Show("Final destination is missing!");
                        return;
                    }
                }

                if (lsbIn.Items.Count > 0) //Var lsbIn.ListCount
                {
                    m_tShippingParam.lContainerFinalNumber = 1;
                }
                else
                {
                    m_tShippingParam.lContainerFinalNumber = 0;
                }

                xlApp.ScreenUpdating = false;
                xlApp.Calculation = XlCalculation.xlCalculationManual;

                if (m_tShippingParam.lContainerFinalNumber > 0)
                {
                    if (m_lFreight1 > 0) //Hér er kallað á sprQuote. Þarf að útfæra það fyrst í Quote forminu
                    {
                        //dCommission = (frmQuote.sprQuote.TextMatrix(m_lFreight1, 4) - frmQuote.sprQuote.TextMatrix(m_lFreight1, 10)) / frmQuote.sprQuote.TextMatrix(m_lFreight1, 4);

                        //frmQuote.sprQuote.TextMatrix(m_lFreight1, 4) = m_tShippingParam.dContainerFinalPrice.ToString("#,##0"); //Price
                        //frmQuote.sprQuote.TextMatrix(m_lFreight1, 21) = m_tShippingParam.dContainerFinalPrice.ToString("#,##0"); //Listprice
                        //frmQuote.sprQuote.TextMatrix(m_lFreight1, 5) = m_tShippingParam.lContainerFinalNumber;
                        //frmQuote.sprQuote.TextMatrix(m_lFreight1, 10) = (m_tShippingParam.dContainerFinalPrice - (m_tShippingParam.dContainerFinalPrice * dCommission)).ToString("#,##0"); //TransferPrice
                    }
                    else
                    {
                        if (!(FreightItem("800-0000-freight_1")))
                        {
                            MessageBox.Show("Villa, ekki - FreightItem(800-0000-freight_1)");
                        }
                    }
                }
                else
                {
                    if (m_lFreight1 > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lFreight1); //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                }

                if (m_tShippingParam.l40ContainerNumberUsa > 0)
                {
                    if (m_lFreight2 > 0)
                    {
                        //frmQuote.sprQuote.TextMatrix(m_lFreight2, 5) = m_tShippingParam.l40ContainerNumberUsa; //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                    else
                    {
                        if (!(FreightItem("800-0000-freight_2")))
                        {
                            MessageBox.Show("Villa, - ekki - FreightItem(800-0000-freight_2)");
                        }
                    }
                }
                else
                {
                    if (m_lFreight2 > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lFreight2); //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                }

                if (m_tShippingParam.l20ContainerNumberUsa > 0)
                {
                    if (m_lFreight3 > 0)
                    {
                        //frmQuote.sprQuote.TextMatrix(m_lFreight3, 5) = m_tShippingParam.l20ContainerNumberUsa; //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                    else
                    {
                        if (!(FreightItem("800-0000-freight_3")))
                        {
                            MessageBox.Show("Villa, ekki FreightItem(800-0000-freight_2)");
                        }
                    }
                }
                else
                {
                    if (m_lFreight3 > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lFreight3); //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                }

                if (m_tShippingParam.l40ContainerNumberEurope > 0)
                {

                    if (optFCA.Checked)
                    {

                        if (m_lFreight9 > 0)
                        {
                            //frmQuote.sprQuote.TextMatrix(m_lFreight9, 5) = m_tShippingParam.l40ContainerNumberEurope; //sprQuote, þarf að útfæra það í Quote fyrst
                        }
                        else
                        {
                            if (!(FreightItem("800-0000-freight_9")))
                            {
                                MessageBox.Show("Villa, ekki FreightItem(800-0000-freight_2)");
                            }
                        }
                    }
                    else if (optFOB.Checked)
                    {
                        if (m_lFreight7 > 0)
                        {
                            //frmQuote.sprQuote.TextMatrix(m_lFreight7, 5) = m_tShippingParam.l40ContainerNumberEurope; //sprQuote, þarf að útfæra það í Quote fyrst
                        }
                        else
                        {
                            if (!(FreightItem("800-0000-freight_7")))
                            {
                                MessageBox.Show("Villa, ekki FreightItem(800-0000-freight_2)");
                            }
                        }
                    }
                    else
                    {
                        if (m_lFreight4 > 0)
                        {
                            //frmQuote.sprQuote.TextMatrix(m_lFreight4, 5) = m_tShippingParam.l40ContainerNumberEurope; //sprQuote, þarf að útfæra það í Quote fyrst
                        }
                        else
                        {
                            if (!(FreightItem("800-0000-freight_4")))
                            {
                                MessageBox.Show("Villa, ekki - FreightItem(800-0000-freight_4)");
                            }
                        }
                    }
                }
                else
                {
                    if (m_lFreight4 > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lFreight4); //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                    if (m_lFreight7 > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lFreight7); //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                    if (m_lFreight9 > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lFreight9); //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                }

                if (m_tShippingParam.l20ContainerNumberEurope > 0)
                {
                    if (optFCA.Checked)
                    {

                        if (m_lFreight5 > 0)
                        {
                            //frmQuote.sprQuote.RemoveItem(m_lFreight5); //sprQuote, þarf að útfæra það í Quote fyrst
                        }
                        if (m_lFreight6 > 0)
                        {
                            //frmQuote.sprQuote.RemoveItem(m_lFreight6); //sprQuote, þarf að útfæra það í Quote fyrst
                        }

                        if (m_lFreight8 > 0)
                        {
                            // frmQuote.sprQuote.TextMatrix(m_lFreight8, 5) = m_tShippingParam.l20ContainerNumberEurope; //sprQuote, þarf að útfæra það í Quote fyrst
                        }
                        else
                        {
                            if (!(FreightItem("800-0000-freight_8")))
                            {
                                MessageBox.Show("Villa, ekki - FreightItem(800-0000-freight_8)");
                            }
                        }
                    }
                    else if (optFOB.Checked)
                    {

                        if (m_lFreight5 > 0)
                        {
                            //frmQuote.sprQuote.RemoveItem(m_lFreight5); //sprQuote, þarf að útfæra það í Quote fyrst
                        }
                        if (m_lFreight8 > 0)
                        {
                            //frmQuote.sprQuote.RemoveItem(m_lFreight8); //sprQuote, þarf að útfæra það í Quote fyrst
                        }

                        if (m_lFreight6 > 0)
                        {
                            //frmQuote.sprQuote.TextMatrix(m_lFreight6, 5) = m_tShippingParam.l20ContainerNumberEurope; //sprQuote, þarf að útfæra það í Quote fyrst
                        }
                        else
                        {
                            if (!(FreightItem("800-0000-freight_6")))
                            {
                                MessageBox.Show("Villa, ekki - FreightItem(800-0000-freight_8)");
                            }
                        }
                    }
                    else
                    {
                        if (m_lFreight8 > 0)
                        {
                            //frmQuote.sprQuote.RemoveItem(m_lFreight8); //sprQuote, þarf að útfæra það í Quote fyrst
                        }
                        if (m_lFreight6 > 0)
                        {
                            //frmQuote.sprQuote.RemoveItem(m_lFreight6); //sprQuote, þarf að útfæra það í Quote fyrst
                        }

                        if (m_lFreight5 > 0)
                        {
                            //frmQuote.sprQuote.TextMatrix(m_lFreight5, 5) = m_tShippingParam.l20ContainerNumberEurope; //sprQuote, þarf að útfæra það í Quote fyrst
                        }
                        else
                        {
                            if (!(FreightItem("800-0000-freight_5")))
                            {
                                MessageBox.Show("Villa, ekki - FreightItem(800-0000-freight_8)");
                            }
                        }
                    }
                }
                else
                {
                    if (m_lFreight5 > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lFreight5); //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                    if (m_lFreight6 > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lFreight6); //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                    if (m_lFreight8 > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lFreight8); //sprQuote, þarf að útfæra það í Quote fyrst
                    }
                }
                xlWorkBook.CustomDocumentProperties("FirstPort").Value = m_tShippingParam.sFirstPort;
                xlWorkBook.CustomDocumentProperties("FirstPort").Value = m_tShippingParam.sFirstPort;
                xlWorkBook.CustomDocumentProperties("FinalDest").Value = m_tShippingParam.sFinalDest;
                xlWorkBook.CustomDocumentProperties("ShippingStatus").Value = m_tShippingParam.sShippingFormStatus;
                xlWorkBook.CustomDocumentProperties("DeliveryTerms").Value = m_tShippingParam.sDeliveryTerms;

                if (m_tShippingParam.lCIFNumber == 1)
                {
                    if (m_tShippingParam.dEquipmentCost > 0)
                    {
                        if (m_lMarelIns > 0)
                        {
                            //dCommission = (frmQuote.sprQuote.TextMatrix(m_lMarelIns, 4) - frmQuote.sprQuote.TextMatrix(m_lMarelIns, 10)) / frmQuote.sprQuote.TextMatrix(m_lMarelIns, 4);
                            //frmQuote.sprQuote.TextMatrix(m_lMarelIns, 4) = MarelInsCost(1).ToString("#,##0");
                            //frmQuote.sprQuote.TextMatrix(m_lCTfreight, 10) = (MarelInsCost(1) - (MarelInsCost(1) * dCommission)).ToString("#,##0"); //TransferPrice
                        }
                        else
                        {
                            if (!(FreightItem("800-0000-ins")))
                            {
                                MessageBox.Show("Villa, ekki - FreightItem(800-0000-freight_8)");
                            }
                        }
                    }

                    if ((m_tShippingParam.sFirstPort.Trim(' ') == "") & (m_tShippingParam.sFinalDest.Trim(' ') != ""))
                    {
                        xlWorkBook.Sheets["Output"].Cells[1, cItotal] = ("Delivery terms: " + m_tShippingParam.sDeliveryTerms + " " + m_tShippingParam.sFinalDest);
                    }
                    else if ((m_tShippingParam.sFinalDest.Trim(' ') == "") & (m_tShippingParam.sFirstPort.Trim(' ') != ""))
                    {
                        xlWorkBook.Sheets["Output"].Cells[1, cItotal] = "Delivery terms: " + m_tShippingParam.sDeliveryTerms + " " + m_tShippingParam.sFirstPort;
                    }
                    else if ((m_tShippingParam.sFinalDest.Trim(' ') != "") & (m_tShippingParam.sFirstPort.Trim(' ') != ""))
                    {
                        xlWorkBook.Sheets["Output"].Cells[1, cItotal] = "Delivery terms: " + m_tShippingParam.sDeliveryTerms + " " + m_tShippingParam.sFinalDest + " via " + m_tShippingParam.sFirstPort;
                    }
                }
                else if (m_tShippingParam.lCIFNumber == 0)
                {
                    if ((m_tShippingParam.sFirstPort.Trim(' ') == "") & (m_tShippingParam.sFinalDest.Trim(' ') != ""))
                    {
                        xlWorkBook.Sheets["Output"].Cells[1, cItotal] = "Delivery terms: " + m_tShippingParam.sDeliveryTerms + " " + m_tShippingParam.sFinalDest;
                    }
                    else if ((m_tShippingParam.sFinalDest.Trim(' ') == "") & (m_tShippingParam.sFirstPort.Trim(' ') != ""))
                    {
                        xlWorkBook.Sheets["Output"].Cells[1, cItotal] = "Delivery terms: " + m_tShippingParam.sDeliveryTerms + " " + m_tShippingParam.sFirstPort;
                    }
                    else if ((m_tShippingParam.sFinalDest.Trim(' ') != "") & (m_tShippingParam.sFirstPort.Trim(' ') != ""))
                    {
                        xlWorkBook.Sheets["Output"].Cells[1, cItotal] = "Delivery terms: " + m_tShippingParam.sDeliveryTerms + " " + m_tShippingParam.sFinalDest + " via " + m_tShippingParam.sFirstPort;
                    }
                    if (m_lMarelIns > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lMarelIns);
                    }
                }
                else if (m_tShippingParam.lCIFNumber == -1)
                {
                    if (m_tShippingParam.sDeliveryTerms == "FOB")
                    {
                        xlWorkBook.Sheets["Output"].Cells[1, cItotal] = "Delivery terms: " + m_tShippingParam.sDeliveryTerms + " " + m_tShippingParam.sFirstPort; // " Reykjavik"
                    }
                    else
                    {
                        xlWorkBook.Sheets["Output"].Cells[1, cItotal] = "Delivery terms: " + m_tShippingParam.sDeliveryTerms + " " + m_tShippingParam.sFirstPort; // " Marel"
                    }

                    if (m_lMarelIns > 0)
                    {
                        //frmQuote.sprQuote.RemoveItem(m_lMarelIns);
                    }
                }
                //        Worksheets("Output").Protect Password:=""

                for (l = 2; l <= 25; l++)
                {
                    xlWorkBook.Worksheets["SolutionC"].Cells[l, 11] = "";
                    xlWorkBook.Worksheets["SolutionC"].Cells[l, 12] = "";

                }
                for (l = 0; l < lsbIn.Items.Count; l++) // 11.10.2012
                {
                    rFundin = xlWorkBook.Worksheets["SolutionC"].Range("A1:A30").Find(lsbIn.Items[1], XlFindLookIn.xlValues);


                    if (rFundin == null)
                    {
                    }
                    else
                    { //Það þarf að athuga þetta
                        //lRow = rFundin.Row;
                        xlWorkBook.Worksheets["SolutionC"].Cells[lRow, 11] = lsbIn.SelectedItems[1];//lsbIn.List(l, 1); //lsbIn.List(l,1);
                        xlWorkBook.Worksheets["SolutionC"].Cells[lRow, 12] = lsbIn.SelectedItems[2];//lsbIn.List(l, 1); //lsbIn.List(l,2);
                    }
                }
                xlApp.Calculation = XlCalculation.xlCalculationAutomatic;
                xlApp.ScreenUpdating = true;

                //Unload this;
                //return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            xlApp.ScreenUpdating = true;
            MessageBox.Show("Villa");
        }

        //Þetta er option takki sem ég finn hvergi í VB - Bætti honum inn í formið, þarf væntanlega að fela hann eða eitthvað.
        private void optCifFreight_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                frmFinalDest.Enabled = true;
                frmReg.Top = 258;
                //frmShippingCost.Height = 317; //Þetta er Freight formið sjálft    
                //freightForm.Size = new Size(317, freightForm.Size.Height);//Ætti að breyta stærðinni á forminu.
                if (optCFRFreight.Checked)
                {
                    m_tShippingParam.lCIFNumber = 0;
                    m_tShippingParam.sDeliveryTerms = "CFR";
                    FirstPortView("CFR");
                }
                CalculateCIFCost();
                CalculateShippingCost();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        } 

        //Option takki á Freight forminu.
        private void optCIP_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (optCIP.Checked)
            {
                m_tShippingParam.lCIFNumber = 1;
                m_tShippingParam.sDeliveryTerms = "CIP";
                FirstPortView("CIP");
            }
            CalculateCIFCost();
            CalculateShippingCost();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //Option takki í Freight forminu
        private void optCPT_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                frmDeliveryTerms2.Enabled = false;
                chkCalShipping.Enabled = true;
                frmDeliveryTerms.Enabled = true;
                frmFirstPort.Enabled = false;
                frmFinalDest.Enabled = true;

                if (optCPT.Checked)
                {
                    m_tShippingParam.sDeliveryTerms = "CPT";
                    m_tShippingParam.lCIFNumber = 0;
                    FirstPortView("CPT");
                }
                CalculateCIFCost();
                CalculateShippingCost();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            } 
        }

        //Option takki í Freight forminu     
        private void optDAP_CheckedChanged(object sender, EventArgs e)
        {
            frmFinalDest.Visible = true;
            frmReg.Top = 258;
            //frmShippingCost.Height = 317; - Þetta er Freight formið sjálft
            //freightForm.Size = new Size(317, freightForm.Size.Height);//Ætti að breyta stærðinni á forminu.

            if (optDAP.Checked)
            {
                m_tShippingParam.sDeliveryTerms = "DAP";
                m_tShippingParam.lCIFNumber = 0;
                FirstPortView("DAP");
            }
            CalculateCIFCost();
            CalculateShippingCost();
        }
        
        //Option takki í Freight forminu
        private void optFCA_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                frmReg.Top = 144;
                if (optFCA.Checked)
                {
                    m_tShippingParam.sDeliveryTerms = "FCA";
                    m_tShippingParam.lCIFNumber = -1;
                    FirstPortView("FCA");
                }
                CalculateCIFCost();
                CalculateFirstPortCost();
                CalculateShippingCost();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //Það er kallað í þetta fall úr optCFRFreight_Click
        private void FirstPortView(object sTerms) // Tilbúið 12.10.2012
        {
            try
            {
                if (sTerms == "FCA")
                {
                    chkCalShipping.Enabled = false;
                    //frmFirstPort.Visible = true;
                    frmFirstPort.Text = "FCA cost";
                    optFirstPortEurope.Checked = false;
                    //optFirstPortEurope.Visible = false;
                    optFirstPortUS.Checked = false;
                    //optFirstPortUS.Visible = false;
                    cboFirstPort.Items.Clear();

                    //Fellivalmyndaboxið fyrir First Port
                    cboFirstPort.Items.Add("Marel(AAR)");
                    cboFirstPort.Items.Add("Marel(BOH)");
                    cboFirstPort.Items.Add("Marel(BOX)");
                    cboFirstPort.Items.Add("Marel(COL)");
                    cboFirstPort.Items.Add("Marel(GRB)");
                    cboFirstPort.Items.Add("Marel(Nitra)");
                    cboFirstPort.Items.Add("Marel(NRS)");
                    cboFirstPort.Items.Add("Marel(NRW)");
                    cboFirstPort.Items.Add("Marel(OSS)");
                    cboFirstPort.Items.Add("Marel(SEA)");
                    cboFirstPort.Items.Add("Marel(SGP)");
                }
                else if (sTerms == "FOB")
                {
                    chkCalShipping.Enabled = false;
                    //frmFirstPort.Visible = true;
                    frmFirstPort.Text = "FOB cost";

                    //optFirstPortEurope.Visible = false;
                    //optFirstPortUS.Visible = false;
                    cboFirstPort.Items.Clear();
                    cboFirstPort.Items.Add("Aarhus(DK)");
                    cboFirstPort.Items.Add("Immingham(GB)");
                    cboFirstPort.Items.Add("Rotterdam(NL)");
                    cboFirstPort.Items.Add("Reykjavik(IS)");
                    cboFirstPort.Items.Add("Singapore(SG)");
                }
                else
                {
                    if (!optFirstPortUS.Checked)
                    {
                        optFirstPortEurope.Checked = true;
                        optFirstPortEurope_CheckedChanged(this, EventArgs.Empty);
                    }
                    chkCalShipping.Enabled = true;
                    chkCalShipping_CheckedChanged(this, EventArgs.Empty);
                    frmFirstPort.Text = "Shipping cost from Iceland";
                    //optFirstPortEurope.Visible = true;
                    //optFirstPortUS.Visible = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //Europe optiontakkinn í Freight
        private void optFirstPortEurope_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                string sPort = null;
                sPort = cboFirstPort.Text;
                if (optFirstPortEurope.Checked)
                {
                    cboFirstPort.Items.Clear();
                    cboFirstPort.Items.Add("Aarhus(DK)");
                    cboFirstPort.Items.Add("Fredriksstad(NO)");
                    cboFirstPort.Items.Add("Gautaborg(SE)");
                    cboFirstPort.Items.Add("Hamborg(DE)");
                    cboFirstPort.Items.Add("Helsingborg(SE)");
                    cboFirstPort.Items.Add("Immingham(GB)");
                    cboFirstPort.Items.Add("Rotterdam(NL)");
                    cboFirstPort.Items.Add("Reykjavik(IS)");
                    cboFirstPort.Items.Add("Sortland(NO)");
                    cboFirstPort.Items.Add("Thorshofn(FO)");

                    cboFirstPort.Text = (sPort == "Marel") ? "" : sPort;
                    m_tShippingParam.l20ContainerNumberEurope = Convert.ToInt32((txt20FeetContainer.Text == "") ? null : txt20FeetContainer.Text);
                    m_tShippingParam.l40ContainerNumberEurope = Convert.ToInt32((txt40feetContainer.Text == "") ? null : txt40feetContainer.Text);
                }

                CalculateFirstPortCost();
                CalculateShippingCost();
                //return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }  
        }

        //US option takkinn í Freight
        private void optFirstPortUS_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (optFirstPortUS.Checked)
                {
                    cboFirstPort.Items.Clear();
                    cboFirstPort.Items.Add("Argentia(CA)");
                    cboFirstPort.Items.Add("Everett/ Boston(US)");
                    cboFirstPort.Items.Add("Halifax(CA)");
                    cboFirstPort.Items.Add("Norfolk VA(US)");

                    m_tShippingParam.l20ContainerNumberUsa = Convert.ToInt32((txt20FeetContainer.Text == "") ? null : txt20FeetContainer.Text);
                    m_tShippingParam.l40ContainerNumberUsa = Convert.ToInt32((txt40feetContainer.Text == "") ? null : txt40feetContainer.Text);
                }
                CalculateFirstPortCost();
                CalculateShippingCost();
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            } 
        }

        //Fob option takkinn í Freight
        private void optFOB_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                frmFinalDest.Visible = false;
                frmReg.Top = 144;
                //frmShippingCost.Height = 203; - Þetta er Freight formið sjálft
                //freightForm.Size = new Size(203, freightForm.Size.Height);//Ætti að breyta stærðinni á forminu.

                if (optFOB.Checked)
                {
                    m_tShippingParam.sDeliveryTerms = "FOB";
                    m_tShippingParam.lCIFNumber = -1;

                    FirstPortView("FOB");
                }

                CalculateCIFCost();
                CalculateFirstPortCost();
                CalculateShippingCost();
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            } 
        } 

        //Option takkinn RegOffer í Freight
        private void optRegOffer_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                //Þegar smellt er á þennan takka þá breytast valmöguleikarnir fyrir neðan.
                frmDeliveryTerms.Enabled = false;
                frmDeliveryTerms2.Enabled = true;
                frmFirstPort.Enabled = true;
                frmFinalDest.Enabled = false;

                CalculateShippingCost();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //optRegOffer2 takkinn í Freight - Sea/Land/Air
        private void optRegOffer2_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
            //    optFOB.Checked = false;
            //    optCFRFreight.Checked = false;
            //    optCifFreight.Checked = false;

                frmDeliveryTerms.Enabled = true;
                frmDeliveryTerms2.Enabled = false;
                frmFirstPort.Enabled = true;
                frmFinalDest.Enabled = false;

                CalculateShippingCost();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //Ef það verður breyting á txt20FeetContainer 
        private void txt20FeetContainer_Change()//Tilbúið, 12.10.2012
        {
            try
            {
                if (optFCA.Checked || optFOB.Checked)
                {
                    m_tShippingParam.l20ContainerNumberEurope = Convert.ToInt32((txt20FeetContainer.Text == "") ? null : txt20FeetContainer.Text);
                    m_tShippingParam.l20ContainerNumberUsa = 0;
                }
                else
                {
                    if (optFirstPortEurope.Checked)
                    {
                        m_tShippingParam.l20ContainerNumberEurope = Convert.ToInt32((txt20FeetContainer.Text == "") ? null : txt20FeetContainer.Text);
                        m_tShippingParam.l20ContainerNumberUsa = 0;
                    }
                    else if (optFirstPortUS.Checked)
                    {
                        m_tShippingParam.l20ContainerNumberUsa = Convert.ToInt32((txt20FeetContainer.Text == "") ? null : txt20FeetContainer.Text);
                        m_tShippingParam.l20ContainerNumberEurope = 0;
                    }
                }

                CalculateFirstPortCost();
                CalculateShippingCost();

                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //Ef það verður breyting á txt40FeetContainer 
        private void txt40feetContainer_Change() //Tilbúið 12.10.2012 - Þarf að athuga með þessa return statement
        {
            try
            {
                if (optFCA.Checked || optFOB.Checked)
                {
                    m_tShippingParam.l40ContainerNumberEurope = Convert.ToInt32((txt40feetContainer.Text == "") ? null : txt40feetContainer.Text);
                    m_tShippingParam.l40ContainerNumberUsa = 0;
                }
                else
                {

                    if (optFirstPortEurope.Checked)
                    {
                        m_tShippingParam.l40ContainerNumberEurope = Convert.ToInt32((txt40feetContainer.Text == "") ? null : txt40feetContainer.Text);
                        m_tShippingParam.l40ContainerNumberUsa = 0;
                    }
                    else if (optFirstPortUS.Checked)
                    {
                        m_tShippingParam.l40ContainerNumberUsa = Convert.ToInt32((txt40feetContainer.Text == "") ? null : txt40feetContainer.Text);
                        m_tShippingParam.l40ContainerNumberEurope = 0;
                    }
                }
                CalculateFirstPortCost();
                CalculateShippingCost();

                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //Ef það verður breyting á txt40FeetContainer 
        private void txtFinalAdress_TextChanged(object sender, EventArgs e) //Tilbúið 12.10.2012 - Þarf að athuga með þessa return statement
        {
            try
            {
                m_tShippingParam.sFinalDest = txtFinalAdress.Text;
                //return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void CalculateFirstPortCost() // Tilbúið - 12.10.2012
        {
            //Það er verið að vísa í einhverja label hérna sem ég þekki ekki
            try
            {
                if (optFCA.Checked)
                {
                    lblFirstPortCost.Text = Convert.ToString(m_tShippingParam.d20FCA * m_tShippingParam.l20ContainerNumberEurope + m_tShippingParam.d40FCA * m_tShippingParam.l40ContainerNumberEurope);
                }
                else if (optFOB.Checked)
                {
                    lblFirstPortCost.Text = Convert.ToString(m_tShippingParam.d20FOB * m_tShippingParam.l20ContainerNumberEurope + m_tShippingParam.d40FOB * m_tShippingParam.l40ContainerNumberEurope);
                }
                else
                {
                    if (optFirstPortEurope.Checked)
                    {
                        lblFirstPortCost.Text = Convert.ToString(m_tShippingParam.d20ContainerPriceEurope * m_tShippingParam.l20ContainerNumberEurope + m_tShippingParam.d40ContainerPriceEurope * m_tShippingParam.l40ContainerNumberEurope);
                    }
                    else if (optFirstPortUS.Checked)
                    {

                        lblFirstPortCost.Text = Convert.ToString(m_tShippingParam.d20ContainerPriceUsa * m_tShippingParam.l20ContainerNumberUsa + m_tShippingParam.d40ContainerPriceUsa * m_tShippingParam.l40ContainerNumberUsa);
                    }
                    else
                    {
                        lblFirstPortCost.Text = "0";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void CalculateCIFCost() // Tilbúið - 12.10.2012
        {
            try
            {
                if (optCifFreight.Checked)
                {
                    //lblCIFFreightCost.Font.Size = 9; - Read only
                    lblCIFFreightCost.Text = Convert.ToString(m_tShippingParam.dCIFprocent * (m_tShippingParam.dEquipmentCost + m_tShippingParam.dAEWDeqCost + m_tShippingParam.dCTeqCost + m_tShippingParam.dDTeqCost));
                    //Það var kallað á Round hér að ofan, sjá til hvort þetta virkar án þess
                    lblCIPcost.Text = Convert.ToString(0);
                }
                else if (optCIP.Checked)
                {
                    //lblCIPcost.Font.Size = 9; -  Read Only
                    lblCIPcost.Text = Convert.ToString(m_tShippingParam.dCIFprocent * (m_tShippingParam.dEquipmentCost + m_tShippingParam.dAEWDeqCost + m_tShippingParam.dCTeqCost + m_tShippingParam.dDTeqCost));
                    lblCIFFreightCost.Text = Convert.ToString(0);
                }
                else
                {
                    lblCIFFreightCost.Text = Convert.ToString(0);
                    lblCIPcost.Text = Convert.ToString(0);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void CalculatePackingHandling() //Sé ekki hvar er kallað í þetta fall.
        {
            try
            {
                MessageBox.Show("Veit ekki hvað þetta fall gerir.");
                //lblPackingHandling.Caption = Convert.ToString(m_tShippingParam.dph1 * m_tShippingParam.lph1 + m_tShippingParam.dph2 * m_tShippingParam.lph2 + m_tShippingParam.dph3 * m_tShippingParam.lph3 + m_tShippingParam.dph4 * m_tShippingParam.lph4);
                //return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        
        private void CalculateShippingCost() //Þetta fall á að virka 11.10.05
        {
            try
            {
                double dFwCost = 0;
                int l = 0;

                for (l = 0; l < lsbIn.Items.Count; l++)
                {
                    dFwCost = dFwCost + Convert.ToDouble(lsbIn.Items.IndexOf(1)); //(l, 2));
                }

                m_tShippingParam.dContainerFinalPrice = dFwCost;
                //lblTotalCost.Text = Convert.ToString(Convert.ToDouble((lblFirstPortCost.Text == "") ? null : lblFirstPortCost.Text) + Convert.ToDouble((lblCIFFreightCost.Text == "") ? null : lblCIFFreightCost.Text) + Convert.ToDouble((lblCIPcost.Text== "") ? null : lblCIPcost.Text) + dFwCost); //Veit ekki hvaðan lblCIFFreightCost kemur
                lblTotalCost.Text = Convert.ToString(dFwCost);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private double BoxCost() //Veit ekki hvernær er kallað í þetta fall.
        {
            //double tempBoxCost = 0;
            //try
            //{
            //    //long l = 0;

            //    //for (l = 1; l <= lsbSelectPacking.ListCount; l++)
            //    //{

            //    //    tempBoxCost = tempBoxCost + lsbSelectPacking.List(l - 1, 2) * lsbSelectPacking.List(l - 1, 3);
            //    //}
            //    //return tempBoxCost;
            //}
            //catch
            //{
            //}
            return 0; //Skilagildi fallsins
        }

        private bool FreightItem(string sItemNumber) // Þarf að skoða vel og fullklára
        {
            bool tempFreightItem = false;
            try
            {
                //string sSql = null;
                //string connectString = null;
                //ADODB.Connection Conn = null;
                //ADODB.Recordset rs = null;
                //double dListprice = 0;
                //double dCommission = 0;
                //int iRow = 0;
                //int lQRow = 0;
                //bool bFreightExist = false;
                //int lL = 0;
                //tempFreightItem = true;
                //connectString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = " + m_sDBpath;

                //Conn = new ADODB.Connection();
                //rs = new ADODB.Recordset();

                //sSql = "Select ID, Item_Number, Item_Name, ListPrice, '1' AS Units, Comment,Supplier_Name, Item_type, TransferPrice, Product_Name, Com_percent from Pricelist where Item_Number = '" + sItemNumber + "'";

                //Conn.Open(connectString);
                //rs.Open sSql, Conn, adOpenKeyset, adLockPessimistic;


                //iRow = frmQuote.sprQuote.Rows;
                //frmQuote.sprQuote.Rows = iRow + 1;
                //while (!rs.EOF)
                //{

                //    for (lQRow = 1; lQRow < frmQuote.sprQuote.Rows; lQRow++)
                //    {
                //        if (frmQuote.sprQuote.TextMatrix(lQRow, 2) == rs.Fields[9].Value)
                //        {
                //            bFreightExist = true;
                //        }
                //    }

                //    if (!bFreightExist)
                //    {
                //        frmQuote.sprQuote.TextMatrix(iRow, 2) = rs.Fields[9].Value;
                //        frmQuote.sprQuote.TextMatrix(iRow, 3) = rs.Fields[9].Value;
                //        frmQuote.sprQuote.TextMatrix(iRow, 11) = rs.Fields[9].Value;
                //        frmQuote.sprQuote.TextMatrix(iRow, 9) = "Freight";
                //        frmQuote.sprQuote.TextMatrix(iRow, 25) = "d";
                //        iRow = frmQuote.sprQuote.Rows;
                //        frmQuote.sprQuote.Rows = iRow + 1;
                //    }

                //    frmQuote.sprQuote.TextMatrix(iRow, 1) = rs.Fields[0].Value; //Id
                //    frmQuote.sprQuote.TextMatrix(iRow, 2) = rs.Fields[1].Value; //Item Number
                //    frmQuote.sprQuote.TextMatrix(iRow, 3) = rs.Fields[2].Value; //Item Name
                //    if (sItemNumber == "800-0000-freight_5")
                //    {
                //        dListprice = rs.Fields[3].Value;
                //    }
                //    else if (sItemNumber == "800-0000-freight_4")
                //    {
                //        dListprice = rs.Fields[3].Value;
                //    }
                //    else if (sItemNumber == "800-0000-freight_3")
                //    {
                //        dListprice = rs.Fields[3].Value;
                //    }
                //    else if (sItemNumber == "800-0000-freight_2")
                //    {
                //        dListprice = rs.Fields[3].Value;
                //    }
                //    else if (sItemNumber == "800-0000-freight_1")
                //    {
                //        dListprice = m_tShippingParam.dContainerFinalPrice;
                //    }
                //    else if (sItemNumber == "800-0000-freight_6")
                //    {
                //        dListprice = rs.Fields[3].Value;
                //    }
                //    else if (sItemNumber == "800-0000-freight_7")
                //    {
                //        dListprice = rs.Fields[3].Value;
                //    }
                //    else if (sItemNumber == "800-0000-freight_8")
                //    {
                //        dListprice = rs.Fields[3].Value;
                //    }
                //    else if (sItemNumber == "800-0000-freight_9")
                //    {
                //        dListprice = rs.Fields[3].Value;
                //    }
                //    else if (sItemNumber == "800-0000-ins")
                //    {
                //        dListprice = MarelInsCost(1);
                //    }

                //    frmQuote.sprQuote.TextMatrix(iRow, 4) = dListprice.ToString("#,##0");
                //    frmQuote.sprQuote.TextMatrix(iRow, 21) = dListprice.ToString("#,##0");
                //    frmQuote.sprQuote.TextMatrix(iRow, 5) = rs.Fields[4].Value; //Units
                //    frmQuote.sprQuote.TextMatrix(iRow, 6) = IsNull(rs.Fields[5].Value) ? "" : rs.Fields[5].Value; //Comment rs.Fields(5).Value
                //    frmQuote.sprQuote.TextMatrix(iRow, 7) = rs.Fields[6].Value; //Supplier
                //    frmQuote.sprQuote.TextMatrix(iRow, 8) = rs.Fields[7].Value; //ItemType
                //    frmQuote.sprQuote.TextMatrix(iRow, 9) = "Freight"; //ProductType
                //    dCommission = rs.Fields[10].Value;
                //    frmQuote.sprQuote.TextMatrix(iRow, 10) = (dListprice - (dListprice * dCommission)).ToString("#,##0"); //TransferPrice
                //    frmQuote.sprQuote.TextMatrix(iRow, 11) = rs.Fields[9].Value; //ProductName
                //    frmQuote.sprQuote.TextMatrix(iRow, 12) = "0";

                //    switch (rs.Fields[7].Value) //ItemType
                //    {
                //        case "S":
                //            frmQuote.sprQuote.TextMatrix(iRow, 25) = "c";
                //            break;
                //        case "F":
                //            frmQuote.sprQuote.TextMatrix(iRow, 25) = "d";
                //            break;
                //    }

                //    rs.MoveNext();
                //}

                //rs = null;
                //Conn = null;

                //return tempFreightItem;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }

            tempFreightItem = false;
            //rs = null;
            //Conn = null;
            return true; // á ekkii að vera 
        }

        private double ContainerPrice(String sItemNumber) //Fullklárað - tilbúið 12.10.2012
        {
            double tempContainerPrice = 0;
            //string sSql = null; //Strengur sem er með sql skipunina

            //double dListprice = 0;
            //double dCommission = 0;
            //int iRow = 0;
            //long lL = 0;
            //String m_sDBpath = null;

            ////New-a CreateConnection úr DataLeyer til að fá aðgang að OleDB tengingunni
            //CreateConnection conni = new CreateConnection();

            ////SQl skipunin
            //sSql = "Select ListPrice, TransferPrice, Com_percent from Pricelist where Item_Number = '" + sItemNumber + "'";
            //DataSet ds = new DataSet();
            //ds = conni.ExecuteSql(sSql);
            //DataRow dtRow;
            //dtRow = ds.Tables[0].Rows[0];

            //for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            //{
            //    tempContainerPrice = Convert.ToDouble(dtRow.ItemArray.GetValue(1));
            //}
            return tempContainerPrice;
        }

        private void chkCalShipping_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                frmFirstPort.Enabled = true;
                frmDeliveryTerms2.Enabled = true;

                if (chkCalShipping.Checked)
                {
                    frmFirstPort.Enabled = true;
                    m_tShippingParam.sShippingFormStatus = "Calculate";
                }

                else
                {
                    optFirstPortEurope.Checked = false;
                    optFirstPortUS.Checked = false;
                    txt20FeetContainer.Text = "";
                    txt40feetContainer.Text = "";
                    lblFirstPortCost.Text = "";
                    cboFirstPort.Text = "";
                    cboFirstPort.Items.Clear();

                    m_tShippingParam.sShippingFormStatus = "Offer";

                    m_tShippingParam.l20ContainerNumberEurope = 0;
                    m_tShippingParam.l20ContainerNumberUsa = 0;
                    m_tShippingParam.l40ContainerNumberEurope = 0;
                    m_tShippingParam.l40ContainerNumberUsa = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

      
    }
}
